﻿@page "/account/confirmemail"
@page "/account/confirmemail/{UserId}"
@using BlazorBoilerplate.Shared.Models.Account

@layout LoginLayout
@inject NavigationManager navigationManager
@inject HttpClient Http
@inject AuthenticationStateProvider authStateProvider
@inject IStringLocalizer<Global> L
@inject IViewNotifier viewNotifier

@if (string.IsNullOrEmpty(UserId))
{
    <MatCard>
        <div class="logo">
            <img src="_content/BlazorBoilerplate.Theme.Material/images/logo.svg" style="width:100px;" /><br />BlazorBoilerplate
            <br />
            <h4>@L["Confirm Email"]</h4>
        </div>
        <EditForm Model="@confirmEmailViewModel" OnValidSubmit="@SendConfirmation">
            <FluentValidationValidator />
            <ValidationSummary />
            <fieldset>
                <div class="form-group">
                    <MatTextField @bind-Value="@confirmEmailViewModel.UserId" Label="UserId" Icon="person" IconTrailing="true" FullWidth="true" Required="true"></MatTextField>
                </div>
                <div class="form-group">
                    <MatTextField @bind-Value="@confirmEmailViewModel.Token" Label="Token" Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true"></MatTextField>
                </div>
                <div class="form-group d-flex justify-content-end">
                    <MatButton Type="submit" Raised="true" Disabled="@disableConfirmButton">@L["Send Confirmation"]</MatButton>
                </div>
            </fieldset>
        </EditForm>
    </MatCard>
}
else
{
    <LoadingBackground ShowLogoBox="true">
        <label>@L["Email confirmation in progress"]</label>
    </LoadingBackground>
}

@code {
    ConfirmEmailViewModel confirmEmailViewModel { get; set; } = new ConfirmEmailViewModel();
    bool disableConfirmButton = false;

    [Parameter]
    public string UserId { get; set; }

    protected override async Task OnInitializedAsync()
    {
        // Blazor does not have query string accessible parameters yet so a hack is needed. Token is too long for path parameter
        var absoluteUrl = navigationManager.Uri;
        var token = absoluteUrl.Substring(absoluteUrl.IndexOf("?token=") + 7);

        if (!string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(token))
        {
            disableConfirmButton = true;
            confirmEmailViewModel = new ConfirmEmailViewModel
            {
                Token = token,
                UserId = this.UserId
            };
            await SendConfirmation();
        }
    }

    async Task SendConfirmation()
    {
        try
        {
            var apiResponse = await ((IdentityAuthenticationStateProvider)authStateProvider).ConfirmEmail(confirmEmailViewModel);

            if (apiResponse.IsSuccessStatusCode)
            {
                viewNotifier.Show(apiResponse.Message, ViewNotifierType.Success);

                navigationManager.NavigateTo("/account/login");
            }
            else
                viewNotifier.Show(apiResponse.Message, ViewNotifierType.Error);
        }
        catch (Exception ex)
        {
            viewNotifier.Show(ex.Message, ViewNotifierType.Error, L["EmailVerificationFailed"]);
        }
    }
}
